feat: support configurable basePath via NEXT_PUBLIC_BASE_PATH#181
Open
shukiv wants to merge 1 commit intobulwarkmail:mainfrom
Open
feat: support configurable basePath via NEXT_PUBLIC_BASE_PATH#181shukiv wants to merge 1 commit intobulwarkmail:mainfrom
shukiv wants to merge 1 commit intobulwarkmail:mainfrom
Conversation
Add support for hosting Bulwark under a sub-path (e.g. /webmail) by
reading basePath from the NEXT_PUBLIC_BASE_PATH env var at build time.
Next.js's basePath config does not automatically prefix client-side
fetch() calls — only navigation. This PR adds a small apiPath() helper
that prepends the basePath to API URLs, and updates all client-side
fetch calls to /api/ routes to use it.
Usage:
NEXT_PUBLIC_BASE_PATH=/webmail npm run build
When NEXT_PUBLIC_BASE_PATH is empty or unset, behavior is unchanged.
Changes:
- next.config.ts: conditionally set basePath from env var, expose it as
NEXT_PUBLIC_BASE_PATH to the client
- lib/api-path.ts: new helper with unit tests
- hooks/, lib/, stores/, components/, app/: wrap 98 fetch('/api/...')
calls in apiPath() (server-side route.ts files not modified — basePath
does not apply to them)
This was referenced Apr 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for hosting Bulwark under a sub-path (e.g.
/webmail) by readingbasePathfrom theNEXT_PUBLIC_BASE_PATHenv var at build time.Motivation
When Bulwark is proxied under a sub-path via reverse proxy (nginx, Traefik, etc.), Next.js's
basePathconfig is required so the server generates correct URLs. However,basePathdoes not automatically prefix client-sidefetch()calls — only navigation. This causes all API requests from the browser to target the wrong URL (/api/...instead of/webmail/api/...), breaking authentication, settings, calendars, plugins, etc.This is documented Next.js behavior but surprising, and the fix is simple: wrap API URLs in a helper that reads the basePath.
Changes
next.config.ts: conditionally setbasePathfromprocess.env.NEXT_PUBLIC_BASE_PATH; expose the value as a public env var so the client can read itlib/api-path.ts: new helper exportingapiPath(path)that prepends basePath (no-op when empty); returns input unchanged if already prefixedlib/api-path.test.ts: unit tests covering empty/unset basePath, happy path, and idempotencyhooks/,lib/,stores/,components/,app/: wrap 98fetch('/api/...')calls inapiPath()across 26 filesServer-side route handlers (
app/api/*/route.ts) were not modified — basePath does not apply to internal routing.Usage
When
NEXT_PUBLIC_BASE_PATHis empty or unset, behavior is unchanged.Test plan
vitest lib/api-path.test.ts)NEXT_PUBLIC_BASE_PATHset/webmailwithNEXT_PUBLIC_BASE_PATH=/webmailNotes